/**
 * MeetPropertyAction.java
 */
package com.prs.crm.action.meeting;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import com.prs.crm.domain.meeting.MeetProperty;
import com.prs.crm.domain.oa.SynodType;
import com.prs.crm.service.meeting.MeetPropertyService;
import com.prs.crm.service.sys.LogService;

/**
 * @author ZhuDongMei
 * @E-mail:1223984339@qq.com
 * @qq:1223984339
 * @version 创建时间：2011-3-16 上午11:12:28 简单说明
 */

public class MeetPropertyAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private MeetProperty meetProperty;
    private MeetPropertyService meetPropertyService;
    private PaginationSupport pagination;
    private Collection<TreeNode> nodes;
	private Integer[] meetPropertyIds;
	private LogService logService;
	 public LogService getLogService() {
		return logService;
	}
	public void setLogService(LogService logService) {
		this.logService = logService;
	}
	public Collection<TreeNode> getNodes() {
			return nodes;
		}
		public void setNodes(Collection<TreeNode> nodes) {
			this.nodes = nodes;
		}
	public MeetProperty getMeetProperty() {
		return meetProperty;
	}
	public void setMeetProperty(MeetProperty meetProperty) {
		this.meetProperty = meetProperty;
	}
	public MeetPropertyService getMeetPropertyService() {
		return meetPropertyService;
	}
	public void setMeetPropertyService(MeetPropertyService meetPropertyService) {
		this.meetPropertyService = meetPropertyService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getMeetPropertyIds() {
		return meetPropertyIds;
	}
	public void setMeetPropertyIds(Integer[] meetPropertyIds) {
		this.meetPropertyIds = meetPropertyIds;
	}
	@Action("listMeetProperty")
	public String listMeetProperty(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MeetProperty.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMeetPropertyService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("selectMeetProperty")
	public String selectMeetProperty() {
		this.listMeetProperty();
		return SUCCESS;
	}

	@Action(value = "loadMeetPropertyTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadMeetPropertyTree() {
		nodes = new ArrayList<TreeNode>();
		List<MeetProperty> regs = this.getMeetPropertyService().getAllMeetPropertys();
		for (MeetProperty reg : regs) {
			TreeNode node = new TreeNode();
			node.setId(reg.getId().toString());
			node.setText(reg.getName());
			node.setText(reg.getRemark());
			nodes.add(node);
		}
		return SUCCESS;
	}
	@Action("editMeetProperty")
	public String editMeetProperty(){
		return SUCCESS;
	}
	@Action(value="saveMeetProperty",results={
			@Result(name="success",type="redirect",location="listMeetProperty"),
			@Result(name="error",type="chain",location="editMeetProperty")
	})
	public String saveMeetProperty(){
		if(meetProperty.getName()==null){
			this.addActionError("会议性质名称不能为空！");
			return ERROR;
		}
		if (this.getLogService().getSearchName(MeetProperty.class,
				meetProperty.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getMeetPropertyService().save(meetProperty);
		return SUCCESS;
	}
	@Action(value="removeMeetProperty",results={
			@Result(name="success",type="redirect",location="listMeetProperty"),
			@Result(name="error",type="chain",location="listMeetProperty")
	})
	public String removeMeetProperty(){
		if(meetPropertyIds==null){
			this.addActionError("请选择要删除的会议性质！");
			return ERROR;
		}
		for (Integer id : meetPropertyIds) {
			MeetProperty type = this.getMeetPropertyService().get(id);
			if (type.getMeetStarts().size() != 0) {
				this.addActionError("已有会议引用，不能删除");
				return ERROR;
			}
		}
		this.getMeetPropertyService().batchRemove(meetPropertyIds);
		return SUCCESS;
	}
}
